package kassiopeia.agent.seda.stage;

import kassiopeia.agent.internal.metrics.MetricsAggregator;
import kassiopeia.plugin.DataProviderPlugin;
import kassiopeia.plugin.Plugin;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Configurable;

@Configurable(dependencyCheck = true)
public class DataProviderExecutor implements PluginExecutor {

    private final Logger logger = Logger.getLogger(DataProviderExecutor.class);
    private Plugin plugin;
    private String pluginInstanceId;

    public DataProviderExecutor(Plugin plugin) {
        this.plugin = plugin;
        this.pluginInstanceId = plugin.getContext().getPluginInstanceId();
    }

    @Override
    public void execute() {
        logger.info("Starting execution of DataProvider: " + pluginInstanceId);
        try {
            ((DataProviderPlugin) plugin).provideData();
        } catch (Exception e) {
            logger.warn("Error in Data Provider Plugin [" + pluginInstanceId + "] :" + e.getMessage());
            if (MetricsAggregator.isEnabled()) {
                MetricsAggregator.countPluginError(plugin);
            }
        }
        logger.info("Execution of DataProvider: " + pluginInstanceId + " finished");
    }

}
